<?php

namespace plugins\yongtai\model;

use plugins\yongtai\Utils;
use think\Model;


class BaseModel extends Model
{
    protected $error = "";
    protected $plugin_name = "yongtai";
    protected $image_postfix_url;

    function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->image_postfix_url = cmf_get_plugin_config($this->plugin_name)['image_postfix_url'];
    }

    function GetError()
    {
        return $this->error;
    }

    function GetOne($_id)
    {
        return $this->find($_id);
    }

    function EditParam($id, $param_name, $param_value)
    {
        $data = [$param_name => $param_value];
        $res = $this->where('id', $id)->save($data);
        return $res;
    }

    function DelOne($id, $callable = null)
    {
        if ($callable) $callable($id);
        return $this->where('id', $id)->delete();
    }

    function RollBackReturn($error, $model)
    {
        $this->error = $error;
        $model->rollback();
        return false;
    }

    function GetListPage($where = [], $page = 1, $page_size = 12, $order = 'listorder desc,id desc', $field = "*")
    {
        $_res = $this->where($where);
        $res['count'] = $_res->count();
        $res['list'] = $_res->order($order)->field($field);
        if ($page_size) {
            $res['list'] = $res['list']->page($page, $page_size)->select();
        } else {
            $res['list'] = $res['list']->select();
        }
        if ($res['list']) $res['list'] = $res['list']->toArray();
        else $res['list'] = [];
        $res['list'] = Utils::ArrFormatThumb($res['list'], $this->image_postfix_url);
        $res['current'] = intval($page);
        $res['page_size'] = $page_size;
        return $res;
    }

    function GetListData($where = [], $page = 1, $page_size = 12, $order = 'listorder desc,id desc', $field = "*", $to_json = true)
    {
        $_res = $this->where($where);
        $res['count'] = $_res->count();
        $res['list'] = $_res->order($order)->field($field);
        if ($page_size) {
            $res['list'] = $res['list']->page($page, $page_size)->select();
        } else {
            $res['list'] = $res['list']->select();
        }
        if ($res['list']) $res['list'] = $res['list']->toArray();
        else $res['list'] = [];
        $res['list'] = Utils::ArrFormatThumb($res['list'], cmf_get_plugin_config($this->plugin_name)['postfix_image_url']);
        $res['current'] = intval($page);
        $data = [
            "code" => 0,
            "msg" => "",
            "count" => $res['count'],
            "data" => $res['list']
        ];
        return $to_json ? json($data) : $data;
    }

    function GetListAll($where = [], $order = 'listorder desc,id desc', $field = '*', $callable = null)
    {
        $_list = [];
        $_res = $this->where($where);
        $res = $_res->order($order)->field($field)->select()->toArray();
        if ($res) {
            foreach ($res as $item) {
                if ($callable) {
                    $item = $callable($item);
                }
                if (isset($item['more']) and $item['more']) {
                    $item['more'] = unserialize($item['more']);
                }
                $_list[] = $item;
            }
        }
        return Utils::ArrFormatThumb($_list, $this->image_postfix_url);
    }

    function GetFields($id, $fileds = "*")
    {
        $res = $this->find($id);
        if ($fileds != "*") {
            $_res = [];
            $__arr = explode(",", $fileds);
            if (count($__arr) > 1) {
                foreach ($__arr as $field) {
                    $_res[$field] = "";
                    if ($res and isset($res[$field])) {
                        $_res[$field] = $res[$field];
                    }
                }
            } else {
                $_res = "";
                if ($res and isset($res[$fileds]))
                    $_res = $res[$fileds];
                return $_res;
            }
            return $_res;
        } else {
            return $res;
        }
    }
}
